﻿using System;
using UniMana.Utils.Controls;
using System.Linq;
namespace UniMana.Data
{
    public class Personnel_Personnel_SessionInfo
    {
        #region /*Không thay đổi*/
        public bds m_bds = new bds();
        private DBMain m_data = new DBMain();
        private bool m_bExist = true;

        public bool ExistInDataBase
        {
            get { return this.m_bExist; }
        }
        public void Save()
        {
            m_data.m_DCSave.SubmitChanges();
        }
        public void Delete()
        {
            m_bds.DeleteCurrent();
        }
        #endregion /*Không thay đổi*/

        #region [Properties - Thuộc tính]

        public Personnel_Personnel_Session Properties
        {
            get { return m_bds.Current as Personnel_Personnel_Session; }
        }
        #endregion

        public Personnel_Personnel_SessionInfo(
            Guid _gPerssonel_PersonnelKey, 
            int _iDayOfWeekKey, 
            int _iSessionKey, DateTime _dteRegistTime)
        {
            var iQuery = this.m_data.m_DCSave.Personnel_Personnel_Sessions.Where(
                p => p.Personnel_PersonnelKey == _gPerssonel_PersonnelKey && 
                    p.Training_WeekDayKey == _iDayOfWeekKey && 
                    p.Training_SessionKey == _iSessionKey && 
                    p.RegistTime == _dteRegistTime);
            this.m_bds.DataSource = iQuery;
            var row = iQuery.FirstOrDefault();
            this.m_bExist = true;
            if (row == null)
            {
                this.m_bExist = false;
                row = new Personnel_Personnel_Session();
                this.m_bds.Add(row);
                this.m_bds.MoveLast();
            }
        }

        public Personnel_Personnel_SessionInfo(int m_iTraining_SessionKey, string m_strPersonnel_PersonnelKey, DateTime m_dRegistTime, int m_iDayOfWeek)
        {
            var IQuery = m_data.m_DCSave.Personnel_Personnel_Sessions.Where(tb => tb.Training_SessionKey == m_iTraining_SessionKey && tb.Personnel_PersonnelKey.ToString() == m_strPersonnel_PersonnelKey && tb.RegistTime == m_dRegistTime && tb.Training_WeekDayKey == m_iDayOfWeek);
            m_bds.DataSource = IQuery;
            var Row = IQuery.FirstOrDefault();
            if (Row == null)
            {
                Row = new Personnel_Personnel_Session();
                m_bds.Add(Row);
            }
        }
    }
}
